﻿define(function (require, exports, module) {
    var Config = require('config');
    var Request = require('request');
    var m_chatWidth = 400;
    var m_chatWindowHeight = 300;
    var m_chatTextHeight = 25;
    var m_divMain, m_divChat, m_txtChat, m_cboChatType;

    exports.Init = function () {

    };

    exports.Show = function () {
        m_divChat = $('<div>').css({
            width: m_chatWidth, height: m_chatWindowHeight, position: 'absolute', zIndex: 5,
            left: 0, top: Config.GetHeight() - m_chatWindowHeight - m_chatTextHeight,
            background: 'rgba(0, 0, 0, 0.3)', color: 'white', overflowY: 'scroll'
        }).appendTo(document.body);

        m_txtChat = $('<input>').attr('type', 'text').css({
            width: m_chatWidth, position: 'absolute', border: 0, zIndex: 5,
            left: 0, top: Config.GetHeight() - m_chatTextHeight
        }).keydown(function (e) {
            if (e.keyCode == 13) {
                if ($(this).val().trim()) {
                    Request.Ajax({
                        url: 'sendMessage.do',
                        data: { message: $(this).val().trim(), type: 'Generial' }
                    });
                }
                $(this).val('');
            }
        }).appendTo(document.body);

        Request.Comet();
    };

    exports.AddMessage = function (messages) {
        $.each(messages, function () {
            m_divChat.append(
                $('<div>').css('color', this.Type == 1 ? 'red' : 'blue')
                    .html(this.Type == 1 ? this.Value : ['[', this.From, ']:', this.Value].join(''))
            ).scrollTop(m_divChat[0].scrollHeight);
        });
    };
});